﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using Interfaces.CommonData;
using StockInfoFacade;
using StockInfoUI.Commands;


namespace StockInfoUI.MVVM
{
    internal class MainTab : INotifyPropertyChanged
    {
        #region Command variables
        private RelayCommand _CloseCommand;
        private RelayCommand _LoadData;
        private RelayCommand _CreateDatabase;
        private RelayCommand _DropDatabase;
        #endregion

        private readonly BackgroundWorker _bgwLoader = new BackgroundWorker();
        private StockInfoFacade.StockInfoVM _facade;
        

        public MainTab()
        {
            string c = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
            string d = System.Configuration.ConfigurationSettings.AppSettings["dbname"];
            _facade = new StockInfoVM(c, d);
            if (_facade.DBStatus != Interfaces.CommonData.DBStatus.Populated)
                _facade.FromTo = new DateInterval() { DateFrom = DateTime.Today.AddYears(-5), DateTo = DateTime.Today };
            else
            {
                _facade.FromTo = _facade.RetrieveMaxInterval();
            }

            _facade.NumberOfThreads = Convert.ToInt32( System.Configuration.ConfigurationSettings.AppSettings["NumberOfThreads"]);

            _bgwLoader.DoWork += RefreshDatabase;
            _bgwLoader.RunWorkerCompleted += LoadWorkerCompleted;
            _bgwLoader.WorkerReportsProgress = true;
            _bgwLoader.ProgressChanged += LoadWorkerChanged;

            LoadVisibility = "Collapsed";
        }

        public RelayCommand CloseCommand
        {
            get
            {
                _CloseCommand = new RelayCommand(() => Application.Current.MainWindow.Close());
                _CloseCommand.IsEnabled = true;
                return _CloseCommand;
            }
        }
        public DateTime DateFrom 
        {
            get { return _facade.FromTo.DateFrom; }
            set
            {
                DateInterval di = _facade.FromTo;
                di.DateFrom = value;
                _facade.FromTo = di;
            }
        }
        public DateTime DateTo
        {
            get { return _facade.FromTo.DateTo; }
            set
            {
                DateInterval di = _facade.FromTo;
                di.DateTo = value;
                _facade.FromTo = di;
            }

        }

        public string ConnectionString
        {
            get
            {
                return "Connection String => " 
                    + System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
            }

        }
        public string Database {
            get
            {
                return "Database name => " 
                    + System.Configuration.ConfigurationSettings.AppSettings["dbname"];
            }
        }

        public string DBStatus
        {
            get
            {
                return "Database status  => " + _facade.DBStatus.ToString(); 
                
            }
        }

        public RelayCommand LoadData
        {
            get
            {
                _LoadData = new RelayCommand(() =>
                {
                    _bgwLoader.RunWorkerAsync();
                    //_facade.RefreshDatabase(IndexName.SAndP100);
                });
                _LoadData.IsEnabled = true;
                return _LoadData;
            }
        }

        private void RefreshDatabase(object Sender, DoWorkEventArgs e)
        {
            _facade.RefreshDatabase(IndexName.SAndP100, _bgwLoader);
        }

        private void LoadWorkerCompleted(object Sender, RunWorkerCompletedEventArgs e)
        {
            LoadProgress = 0; //When finished loading reset the progress
            LoadVisibility = "Collapsed";
        }

        private void LoadWorkerChanged(object Sender, ProgressChangedEventArgs e)
        {
            LoadProgress = e.ProgressPercentage;
            LoadVisibility = "Overlay";
        }

        private int _loadProgress ;

        public int LoadProgress
        {
            get { return this._loadProgress; }
            set
            {
                this._loadProgress = value;
                FirePropertyChanged("LoadProgress");
            }
        }

        public RelayCommand CreateDatabase
        {
            get
            {
                _CreateDatabase = new RelayCommand(() =>
                {
                    _facade.CreateNewDatabase();
                });
                _CreateDatabase.IsEnabled = true;
                return _CreateDatabase;
            }
            
        }

        public RelayCommand DropDatabase
        {
            get
            {
                _DropDatabase = new RelayCommand(() =>
                {
                    _facade.DropDatabase();
                });
                _DropDatabase.IsEnabled = true;
                return _DropDatabase;
            }

        }


        private string _loadVisibility;

        public string LoadVisibility
        {
            get { return _loadVisibility; }
            set
            {
                _loadVisibility = value;
                FirePropertyChanged("LoadVisibility");
            }
        }


        public event PropertyChangedEventHandler PropertyChanged;

        protected void FirePropertyChanged(String name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;

            if (handler != null)
                handler(this, new PropertyChangedEventArgs(name));
        }
    }
}
